package com.ruyuan.seckilling.inventory.listener;

import com.alibaba.fastjson.JSON;
import com.ruyuan.seckilling.common.QueueKey;
import com.ruyuan.seckilling.common.message.OrderPayMessage;
import com.ruyuan.seckilling.inventory.service.InventoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author zhonghuashishan
 */
@Slf4j
@Component
@RocketMQMessageListener(topic = QueueKey.QUEUE_PAY_ORDER, consumerGroup = "pageOrderGroup")
public class PayOrderListener implements RocketMQListener<String> {

    @Autowired
    private InventoryService inventoryService;

    @Override
    public void onMessage(String messageString) {

        log.info("收到订单支付的消息，mesasge={}", messageString);

        OrderPayMessage message = JSON.parseObject(messageString, OrderPayMessage.class);
        inventoryService.confirmStock(message.getSequence(), message.getActivityId(), message.getSkuId());

        log.info("确认订单支付对应的商品库存");
    }
}
